package com.dtsheep.rxjava.stduy.observable;

import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;

/**
 * FlatMapDemo
 *
 * <p>
 *     flatmap 组合多个数据流
 * </p>
 *
 * @author chuanbai
 * @date 2024/5/17
 */
public class FlatMapDemo {


    /**
     * flatMap 处理元素
     *
     * @param
     * @return
     * @author chuanbai
     * @date ${DATE}
     */
    public void  flatMap01(){
        // 创建 被观察对象 、 发射器 、 cpu密集型调度器
        Observable<String> observable1 = Observable.unsafeCreate((Subscriber<? super String> emmiter) -> {
            emmiter.onNext("创建订单");
            emmiter.onNext("扣减库存");
            emmiter.onCompleted();
        }).subscribeOn(Schedulers.computation());

        Observable<String> observable2 = Observable.unsafeCreate((Subscriber<? super String> emmiter) -> {
            emmiter.onNext("付款");
            emmiter.onNext("支付");
            emmiter.onCompleted();
        }).subscribeOn(Schedulers.computation());

        Observable<Observable<String>> justed = Observable.just(observable1, observable2);

        // 组合 Observable 对象
        // flatMap 返回 Observable 对象
        // map 返回无限制
        Observable<String> observable = observable1
                .flatMap(obj -> Observable.just(observable1.toBlocking().first(), observable2.toBlocking().first()));

    }

    public static void main(String[] args) {

    }
}
